package com.jn.news.ui.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jn.news.R;
import com.jn.news.adapter.NewsClassifyAdapter;
import com.jn.news.model.NewsClassifyDto;
import com.jn.news.utils.ToastUtil;
import com.jn.news.viewmodel.TianxingNewsViewModel;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TianxingNewsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TianxingNewsFragment extends Fragment {


    private TianxingNewsViewModel viewModel;
    private Disposable disposable;
    private RecyclerView mRv;
    private List<NewsClassifyDto> newsClassifyDtos;

    public static TianxingNewsFragment newInstance(int position) {
        TianxingNewsFragment fragment = new TianxingNewsFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_tianxing_news, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        mRv = view.findViewById(R.id.mRv);
    }

    private void initData() {
        // 初始化 ViewModel
        viewModel = new ViewModelProvider(this).get(TianxingNewsViewModel.class);
        viewModel.getNewsClassify().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        response -> {
                            if (response.getCode() == 200) {
                                Collections.reverse(response.getResult().getList().get(0));
                                handleData(response.getResult().getList().get(0));
                            } else {
                                ToastUtil.showToast(getActivity(), "请求失败: " + response.getMessage());
                            }
                        },
                        throwable -> {

                            ToastUtil.showToast(getActivity(), "网络错误");
                        }
                );
    }

    private void handleData(List<NewsClassifyDto> newsClassifyDtos) {
        mRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        NewsClassifyAdapter newsClassifyAdapter = new NewsClassifyAdapter(getActivity());
        mRv.setAdapter(newsClassifyAdapter);
        newsClassifyAdapter.refresh(newsClassifyDtos);
    }

}